﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class gestionComercial_impresion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String pValor = Request.QueryString["idProforma"];
        hdfImprimir.Value = pValor;
        
        String pIdProforma = Request.QueryString["idProyecto"];
        hdfIdProyecto.Value = pIdProforma;
        
        String pIdEtapa = Request.QueryString["idEtapa"];
        hdfIdEtapa.Value = pIdEtapa;

        String pDescEmpresa = Request.QueryString["descEmpresa"];
        hdfEmpresa.Value = pDescEmpresa;
    }
    protected void lnbImprimir_Click(object sender, EventArgs e)
    {
        String empresa = Convert.ToString(hdfEmpresa.Value);
        //String strConnection = "Provider=SQLNCLI;Server=SERVIDOR;Database=EMPRESA;Uid=USUARIO; Pwd=CONTRASEÑA;";
        String strConnection = ConfigurationManager.ConnectionStrings[empresa.ToUpper()].ConnectionString;
        
        //MPE OleDbConnection Connection = new OleDbConnection(strConnection);
        SqlConnection Connection = new SqlConnection(strConnection);
        
        //MPE OleDbDataAdapter DA = new OleDbDataAdapter("usp_proformaCabecera_rptProforma", Connection);
        SqlDataAdapter DA = new SqlDataAdapter("usp_proformaCabecera_rptProforma", Connection);
        
        DA.SelectCommand.CommandType = CommandType.StoredProcedure;
        DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
        DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
        DA.SelectCommand.Parameters.Add("@idProforma", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
        DataSet DS = new DataSet();
        DA.Fill(DS, "T0");

        String wModelo = "";

        switch (empresa.ToLower()){
            case "emarcan":
            case "edvivienda":
            case "emarcan_d":
            case "edvivienda_d":
            
                foreach (DataRow Proforma in DS.Tables[0].Rows)
                {
                    String ModeloReporte = Convert.ToString(Proforma["idTpoProforma"]);
                    if (ModeloReporte != "") {
                        if (ModeloReporte != "0") {
                        wModelo = "M" + ModeloReporte;
                        }

                    }
                }
        break;

            case "epadova":
            case "eurban":
            case "egrp":
            case "eparametros":
            case "eacuario":
            case "eenacorp":
            case "epadova_d":
            case "eurban_d":
            case "egrp_d":
            case "eparametros_d":
            case "eacuario_d":
            case "eenacorp_d":
                // MPE DA = new OleDbDataAdapter("usp_simuladorCtas_rptProforma", Connection);
                DA = new SqlDataAdapter("usp_simuladorCtas_rptProforma", Connection);
                DA.SelectCommand.CommandType = CommandType.StoredProcedure;
                // DA.SelectCommand.Parameters.Add("@idProyecto", OleDbType.Integer).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
                DA.SelectCommand.Parameters.Add("@idProforma", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
                DA.Fill(DS, "T1");
                break;
        }
        switch (empresa.ToLower())
        {
            case "epazcen":
            case "epazcen2":
            case "epazcen3":
            case "epazcenpr":
            case "eedifica":
                // MPE DA = new OleDbDataAdapter("usp_simuladorCtas_rptProforma", Connection);
                DA = new SqlDataAdapter("usp_proforma_descuentos", Connection);
                DA.SelectCommand.CommandType = CommandType.StoredProcedure;
                // DA.SelectCommand.Parameters.Add("@idProyecto", OleDbType.Integer).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
                DA.SelectCommand.Parameters.Add("@idProforma", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
                DA.Fill(DS, "T2");
                break;
        }
        switch (empresa.ToLower())
        {
            case "evalora":
            case "ematellini":
            case "evalora_d":
            case "ematellini_d":
            //case "epazcenpr":
                DA = new SqlDataAdapter("usp_proformaCabecera_rptProforma1", Connection);
                DA.SelectCommand.CommandType = CommandType.StoredProcedure;
                DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
                DA.SelectCommand.Parameters.Add("@idProforma", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
                DA.Fill(DS, "T3");
                break;
        }
        String repFilePath = null;
        repFilePath = Server.MapPath("~/gestionComercial/rptProforma/proforma" + empresa.ToString() + hdfIdProyecto.Value.ToString() + wModelo + ".rpt");
        if (System.IO.File.Exists(repFilePath))
        {                       
            ReportDocument repDoc1 = new ReportDocument();
            repDoc1.Load(repFilePath);
            repDoc1.SetDataSource(DS.Tables["T0"]);
            ReportDocument repDoc = repDoc1;
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            switch (empresa.ToLower())
            {
                case "epadova":
                case "eurban":
                case "egrp":
                case "eparametros":
                case "eacuario":
                case "eenacorp":
                case "epadova_d":
                case "eurban_d":
                case "egrp_d":
                case "eparametros_d":
                case "eacuario_d":
                case "eenacorp_d":
                //case "epazcenpr":
                    SubreportObject subreportObject;
                    ReportDocument subReporte;
                    subreportObject = repDoc1.ReportDefinition.ReportObjects["Subreport1"] as SubreportObject;
                    subReporte = repDoc1.OpenSubreport(subreportObject.SubreportName);
                    subReporte.SetDataSource(DS.Tables["T1"]);
                    ParameterValues pvValues = new ParameterValues();
                    ParameterDiscreteValue pdvDiscreteValue = new ParameterDiscreteValue();
                    pdvDiscreteValue.Value = DS.Tables[1].Rows.Count;
                    pvValues.Add(pdvDiscreteValue);
                    ParameterFieldDefinitions pfdDefinitions = repDoc1.DataDefinition.ParameterFields;
                    ParameterFieldDefinition pfdDefinition = pfdDefinitions["display"];
                    pfdDefinition.ApplyCurrentValues(pvValues);
                    break;
            }
            switch (empresa.ToLower())
            {
                case "epazcen":
                case "epazcen2":
                case "epazcen3":
                case "epazcenpr":
                case "eedifica":
                    SubreportObject subreportObject;
                    ReportDocument subReporte;
                    subreportObject = repDoc1.ReportDefinition.ReportObjects["Subreport10"] as SubreportObject;
                    subReporte = repDoc1.OpenSubreport(subreportObject.SubreportName);
                    subReporte.SetDataSource(DS.Tables["T2"]);
                    break;
            }
            switch (empresa.ToLower())
            {
                case "evalora":
                case "ematellini":
                case "evalora_d":
                case "ematellini_d":
                //case "epazcenpr":
                    SubreportObject subreportObject;
                    ReportDocument subReporte;
                    subreportObject = repDoc1.ReportDefinition.ReportObjects["Subreport11"] as SubreportObject;
                    subReporte = repDoc1.OpenSubreport(subreportObject.SubreportName);
                    subReporte.SetDataSource(DS.Tables["T3"]);
                    break;
            }
            try
            {
                repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Proforma");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ex = null;
            }
        }
    }
}
